<!--  -->
<template>
  <el-card style="height: 100vh">
    <el-tree
      :data="menu"
      :props="defaultProps"
      :expand-on-click-node="false"
      show-checkbox
      node-key="id"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="node.level <= 2"
            type="text"
            size="mini"
            @click="() => append(data)"
          >
            新增分类
          </el-button>
          <el-button
            v-if="node.childNodes.length === 0"
            type="text"
            size="mini"
            @click="() => remove(node, data)"
          >
            删除分类
          </el-button>
        </span>
      </span></el-tree
    >

    <el-dialog title="新增分类" :visible.sync="dialogFormVisible">
      <el-form :model="saveData">
        <el-form-item label="分类名称">
          <el-input v-model="saveData.title" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveSortNB">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { getShopSortList, deleteShop, addShopSort } from "@/api/shopSort";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      dialogFormVisible: false,
      menu: [],
      defaultProps: {
        children: "shopSortSonVo",
        label: "title",
      },
      saveData: {
        title: "",
        parentId: 0,
        sort: 0,
      },
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    getShopSortList() {
      getShopSortList().then((res) => {
        this.menu = res.data.list;
      });
    },
    append(data) {
      this.saveData.parentId = data.id;
      this.dialogFormVisible = true;

      console.log("append", data);
    },
    saveSortNB() {
      addShopSort(this.saveData).then((res) => {
        this.dialogFormVisible = false;
        this.$message.success("添加分类成功");
        this.getShopSortList();
      });
    },

    remove(node, data) {
      console.log("remove", data);
      console.log("remove", node);
      this.$confirm("此操作将永久删除该分类, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteShop(data.id).then((res) => {});
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getShopSortList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getShopSortList();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped>
/* @import url(); 引入公共css类*/
</style>